function verify(){
  if($(".school_type").is(":checked") == false){
    alert("You must select at least one school type!");
    return false;
  }
  else{
    return true;
  }
}

$(function() {
  $( "#datepicker" ).datepicker({
    changeMonth: true,
    changeYear: true,
    dateFormat: 'd MM yy',
  });

  $( "#datepicker_visit" ).datepicker({
    changeMonth: true,
    changeYear: true,
    dateFormat: 'd MM yy',
  });

  /*
  $( "#founding_year" ).datepicker({
    changeMonth: true,
    changeYear: true,
    yearRange: '1900:2011',
    dateFormat: 'yy',
  });
  */
});

var total_enrollment = 0;

//<!--condition for membership type-->
function countFees(){
  var total_students = parseInt($("#total_enrollment").val());
  var monthly_tuition = parseInt($("#monthly_tuition").val());
  var type = $("#membership_type").val();
  var fee = 0;
  var currency = '';
  
  // if(total_students <= 75 && total_students >= 0){
    // $('#membership_type option:nth-child(2)').css('display', 'none');
  // }else{
    // $('#membership_type option:nth-child(2)').css('display', 'block');
  // }

  if(type == 'National - 1 Year')
  {
    currency = 'Rp.';
    if(total_students <= 75 && total_students >= 0){
      fee = 450000;
    }
    else if(total_students <= 799 && total_students >= 76)
    {
      switch(monthly_tuition)
      {
        case 1:
          fee = 5000 * total_students;
          break;
        case 2:
          fee = 6000 * total_students;
          break;
        case 3:
          fee = 7000 * total_students;
          break;
      }
    }
    else if(total_students >= 800)
    {
      switch(monthly_tuition)
      {
        case 1:
          fee = 4000000;
          break;
        case 2:
          fee = 4800000;
          break;
        case 3:
          fee = 5600000;
          break;
      }
    }
  }//<!-- national 2 years -->
  else if(type == 'National - 2 Years')
  {
    currency = 'Rp.';
    if(total_students <= 75 && total_students >= 0){
      fee = 0;
      // $('#membership_type option:nth-child(2)').css('display', 'none');
    }
    else if(total_students <= 799 && total_students >= 76)
    {
      switch(monthly_tuition)
      {
        case 1:
          fee = 9000 * total_students;
          break;
        case 2:
          fee = 11000 * total_students;
          break;
        case 3:
          fee = 13000 * total_students;
          break;
      }
    }
    else if(total_students >= 800)
    {
      switch(monthly_tuition)
      {
        case 1:
          fee = 7200000;
          break;
        case 2:
          fee = 8640000;
          break;
        case 3:
          fee = 10080000;
          break;
      }
    }
  }
  else if(type == 'International - 1 Year')
  {
    currency = '$';
    if(total_students <= 25 && total_students >= 0)
    {
      fee = 225;
    }
    else if(total_students <= 599 && total_students >= 26)
    {
      fee = 12*total_students;
    }
    else if(total_students >= 600)
    {
      fee = 5400;
    }
  }

  if(currency == '$')
  {
    $("#membership_price").val(currency+fee+'.00');
  }
  else
  {
    $("#membership_price").val(currency+fee+',-');
  }
  $("#membership_fee").val(fee);
  getTotalFees();
}

//<!--get total fees-->
function getTotalFees(){
  var member_fee  = parseInt($("#membership_fee").val());
  var gift        = parseInt($("#ministries_gift").val());
  var total       = member_fee + gift;
  $("#total_fees").val(total);
}

//<!--get all member count-->
function getTotalEnrollment(){
  var pg      = parseInt($("#pg_students").val());
  var primary = parseInt($("#primary_students").val());
  var junior  = parseInt($("#junior_students").val());
  var senior  = parseInt($("#senior_students").val());

  total_enrollment = pg + primary + junior + senior;
  $("#total_enrollment").val(total_enrollment);
  countFees();
}

//<!--Gift-->
function gift()
{
  // $(".options").toggle();
  // $("#gift").toggle();
}

$(document).ready(function(){
  $('#gift_type_1').click(function(){
    $(".options").css('display', 'none');
    $("#gift").css('display', 'none');
    
    $('#total_fees').val($('#membership_fee').val());
  });
  
  $('#gift_type_2').click(function(){
    $(".options").css('display', 'block');
    $("#gift").css('display', 'table-row');
  });

  countFees();

  var count = 0;
  // checkSchoolType();

  // $(".school_type").click(function(){
    // checkSchoolType();
  // });
});

$(document).ready(function(){
  $(".hasCount").each(function(){
    $(this).val('0');
    $(this).css('width','50px');;
  });

  $(".hasCount").focus(function(){
    if($(this).val() == '0'){
      $(this).val('');
    }
  });

  $(".total").focusout(function(){
    if($(this).val() == ''){
      $(this).val(0);
    }
  });

  $(".student").focusout(function(){
    if($(this).val() != ''){
      var count = 0;

      if($(this).hasClass('pg')){
        $(".student").each(function(){
          if($(this).hasClass('pg')){
            var value = parseInt($(this).val());
            count = count + value;
          }
        });
        $("#pg_students").val(count);
      }
      else if($(this).hasClass('primary')){
        $(".student").each(function(){
          if($(this).hasClass('primary')){
            var value = parseInt($(this).val());
            count = count + value;
          }
        });
        $("#primary_students").val(count);
      }
      else if($(this).hasClass('junior')){
        $(".student").each(function(){
          if($(this).hasClass('junior')){
            var value = parseInt($(this).val());
            count = count + value;
          }
        });
        $("#junior_students").val(count);
      }
      else if($(this).hasClass('senior')){
        $(".student").each(function(){
          if($(this).hasClass('senior')){
            var value = parseInt($(this).val());
            count = count + value;
          }
        });
        $("#senior_students").val(count);
      }

      getTotalEnrollment();
      count = 0;
    }
    else{ $(this).val('0'); }
  });

  $(".teacher1").focusout(function(){
    if($(this).val() != ''){
      var count = 0;

      if($(this).hasClass('pg')){
        $(".teacher1").each(function(){
          if($(this).hasClass('pg')){
            var value = parseInt($(this).val());
            count = count + value;
          }
        });
        $("#pg_teachers1").val(count);
      }
      else if($(this).hasClass('primary')){
        $(".teacher1").each(function(){
          if($(this).hasClass('primary')){
            var value = parseInt($(this).val());
            count = count + value;
          }
        });
        $("#primary_teachers1").val(count);
      }
      else if($(this).hasClass('junior')){
        $(".teacher1").each(function(){
          if($(this).hasClass('junior')){
            var value = parseInt($(this).val());
            count = count + value;
          }
        });
        $("#junior_teachers1").val(count);
      }
      else if($(this).hasClass('senior')){
        $(".teacher1").each(function(){
          if($(this).hasClass('senior')){
            var value = parseInt($(this).val());
            count = count + value;
          }
        });
        $("#senior_teachers1").val(count);
      }

      getTotalEnrollment();
      count = 0;
    }
    else{ $(this).val('0'); }
  });

  $(".teacher2").focusout(function(){
    if($(this).val() != ''){
      var count = 0;

      if($(this).hasClass('pg')){
        $(".teacher2").each(function(){
          if($(this).hasClass('pg')){
            var value = parseInt($(this).val());
            count = count + value;
          }
        });
        $("#pg_teachers2").val(count);
      }
      else if($(this).hasClass('primary')){
        $(".teacher2").each(function(){
          if($(this).hasClass('primary')){
            var value = parseInt($(this).val());
            count = count + value;
          }
        });
        $("#primary_teachers2").val(count);
      }
      else if($(this).hasClass('junior')){
        $(".teacher2").each(function(){
          if($(this).hasClass('junior')){
            var value = parseInt($(this).val());
            count = count + value;
          }
        });
        $("#junior_teachers2").val(count);
      }
      else if($(this).hasClass('senior')){
        $(".teacher2").each(function(){
          if($(this).hasClass('senior')){
            var value = parseInt($(this).val());
            count = count + value;
          }
        });
        $("#senior_teachers2").val(count);
      }

      getTotalEnrollment();
      count = 0;
    }
    else{ $(this).val('0'); }
  });
});

$(document).ready(function(){
  var email = new LiveValidation('Users_email'); email.add( Validate.Email );
  var email = new LiveValidation('kinder_principal_email'); email.add( Validate.Email );
  var email = new LiveValidation('primary_principal_email'); email.add( Validate.Email );
  var email = new LiveValidation('js_principal_email'); email.add( Validate.Email );
  var email = new LiveValidation('sh_principal_email'); email.add( Validate.Email );
  var email = new LiveValidation('church_email'); email.add( Validate.Email );
  var email = new LiveValidation('individual_email'); email.add( Validate.Email );
  var f4 = new LiveValidation('founding_year'); f4.add( Validate.Numericality, { onlyInteger: true } );
});

$(document).ready(function(){
  $('.newsletter input[type="checkbox"]:nth-child(2)').click(function(){
    $('.newsletter input[type="text"]').fadeToggle('fast');
    
    if($('.newsletter input[type="checkbox"]:nth-child(2):checked')){
      $('.newsletter .LV_validation_message').css('display', 'none');
    }
    
    $('.newsletter input[type="checkbox"]:nth-child(2)').attr('required', 'required');
    $('.newsletter input[type="checkbox"]:nth-child(3)').attr('required', 'false');
  });
  
  $('.newsletter input[type="checkbox"]:nth-child(3)').click(function(){
    $('.newsletter input[type="checkbox"]:nth-child(2)').attr('required', 'false');
    $('.newsletter input[type="checkbox"]:nth-child(3)').attr('required', 'required');
  });
  
  var newsletter_email = new LiveValidation('newsletter_email');
  newsletter_email.add( Validate.Presence );
  newsletter_email.add( Validate.Email );
});